// only add update server if it's not being run from cli
if (require.main !== module) {
  require('update-electron-app')({
    logger: require('electron-log')
  })
}
const path = require('path')
// const glob = require('glob')
const fs = require('fs');
const {app, BrowserWindow ,ipcMain} = require('electron')

fs.mkdir('./audioDown',function(err){
    if(err){
        return false;
    }else{
      console.log('已就绪~')
    }
})

const debug = /--debug/.test(process.argv[3])

app.setName('音频资源管理')
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true;
let mainWindow = null

function createMenu(){
  const menu = require('electron').Menu;
    if (process.platform === 'darwin') {
      const template = [
        {
          label: "Application",
          submenu: [
            { label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }}
          ]
        }, 
        {
          label: "Edit",
          submenu: [
            { label:"Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
            { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
            { type: "separator" },
            { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
            { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
            { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
            { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" },
            { type: "separator" },
            { label: "Move To Left", accelerator: "CmdOrCtrl+J", click: ()=>{mainWindow.webContents.send('move-focus', 'left')} },
            { label: "Move To Right", accelerator: "CmdOrCtrl+L", click: ()=>{mainWindow.webContents.send('move-focus', 'right')} },
            { label: "Move To Up", accelerator: "CmdOrCtrl+I", click: ()=>{mainWindow.webContents.send('move-focus', 'up')} },
            { label: "Move To Down", accelerator: "CmdOrCtrl+K", click: ()=>{mainWindow.webContents.send('move-focus', 'down')} },
          ]
        }
      ];
      menu.setApplicationMenu(menu.buildFromTemplate(template))
    } else {
      // menu.setApplicationMenu(null)
    }
}

makeSingleInstance()
function createWindow () {
  const windowOptions = {
    width: 1080,
    minWidth: 680,
    height: 840,
    title: app.getName(),
    // webPreferences:{
    //   webSecurity:false
    // }
  }
  createMenu();
  if (process.platform === 'linux') {
    windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
  }

  mainWindow = new BrowserWindow(windowOptions)
  mainWindow.loadURL(path.join('file://', __dirname, '/index.html'))

  mainWindow.webContents.session.on('will-download',(event,item,webContents) => {
    console.log('./audioDown/'+item.getFilename());
    item.setSavePath('./audioDown/'+item.getFilename());

    item.on('updated', (event, state) => {
      if (state === 'interrupted') {
        console.log('Download is interrupted but can be resumed')
      } else if (state === 'progressing') {
        if (item.isPaused()) {
          console.log('Download is paused')
        } else {
          console.log(`Received bytes: ${item.getReceivedBytes()}`)
        }
      }
    })

    item.once('done', (event, state) => {
      console.log()
      if (state === 'completed') {
        console.log('Download successfully')
      } else {
        console.log(`Download failed: ${state}`)
      }
    })

  })
  // ipcMain.on('download',(event,args)=>{
  //   mainWindow.webContents.downloadURL(args);
  // })

  // Launch fullscreen with DevTools open, usage: npm run debug
  if (debug) {
    mainWindow.webContents.openDevTools()
    mainWindow.maximize()
    require('devtron').install()
  }

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', () => {
  createWindow()
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
// Make this app a single instance app.
//
// The main window will be restored and focused instead of a second window
// opened when a person attempts to launch a second instance.
//
// Returns true if the current version of the app should quit instead of
// launching.
function makeSingleInstance () {
  if (process.mas) return

  app.requestSingleInstanceLock()

  app.on('second-instance', () => {
    if (mainWindow) {
      if (mainWindow.isMinimized()) mainWindow.restore()
      mainWindow.focus()
    }
  })
}